package com.sharekitchen.mapper;

import com.sharekitchen.entity.Order;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Mapper
@Transactional
public interface OrderMapper {

    //获取所有订单信息
    @Select("SELECT * FROM `order` WHERE is_delete=0")
    List<Order> findAllOrder();

    // 查找订单是否存在
    @Select("SELECT COUNT(*)>0 FROM `order` WHERE order_id = #{orderId} AND is_delete = 0")
    Boolean existOrderId(String orderId);

    // 添加订单 订单状态默认都是未支付,支付后调用接口修改状态
    @Insert("INSERT INTO `order`(`order_id`, `user_id`, `room_id`, " +
            "`order_time`, `start_time`, `end_time`)" +
            " VALUES(#{orderId},#{userId},#{roomId},#{orderTime},#{startTime},#{endTime})")
    int addOrder(Order order);

    // 获取订单的信息
    @Select("SELECT * FROM `order` WHERE order_id = #{orderId} AND is_delete = 0")
    Order findOrderById(String orderId);

    //    删除订单信息
    @Update("UPDATE `order` SET is_delete = 1 WHERE order_id = #{orderId} AND is_delete=0")
    int deleteOrderById(String orderId);

    //    修改订单信息
    @Update("UPDATE `order` SET `user_id`=#{userId},`room_id`=#{roomId}, " +
            "`order_status`=#{orderStatus}, `order_comments`=#{orderComments}, `star`=#{star}" +
            "`order_time`=#{orderTime},`start_time`=#{startTime},`end_time`=#{endTime} WHERE order_id = #{orderId} AND is_delete=0")
    int updateOrder(Order order);


}
